package com.example.newempty;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.lang.reflect.Method;

import dalvik.system.DexClassLoader;

/**
 * Created by root on 16-6-6.
 */
public class Utils {
    private static  void deleteFile(File f) {
        if( f == null || !f.exists())
            return;
        System.out.print("\t");
        if( f.isDirectory() ) {
            for(File sub : f.listFiles()) {
                deleteFile(sub);
            }
        }
        f.delete();
    }

    public static void cleanApplicationData(Context context) {
        //delete cache dir
        Log.d("dhy.delete", "cleanInternalCache = " + context.getCacheDir().getAbsolutePath());
        deleteFile(context.getCacheDir());

        //delete db
        Log.d("dhy.delete", "clean db ");
        deleteFile(new File("/data/data/" + context.getPackageName() + "/databases"));

        //delete sharedp
        Log.d("dhy.delete", "clean shared_prefs ");
        String sharedp = "/data/data/" + context.getPackageName() + "/shared_prefs";
        deleteFile(new File(sharedp));

        //delete files
        Log.d("dhy.delete", "clean files");
        deleteFile(context.getFilesDir());

        //以下两个连目录一起删除
        //delete app_RiskStub
        Log.d("dhy.delete", "delete app_RiskStub");
        deleteFile(new File(context.getDir("RiskStub", 0).getAbsolutePath()));
        //delete RiskStub
        Log.d("dhy.delete", "delete RiskStub");
        deleteFile(new File("/data/data/"  + context.getPackageName() + "/RiskStub"));
    }
    
    
    public static DexClassLoader getClassLoader(Context ctx, String dexFilePath) {
    	//通过反射找到pathList的值
    	try {
    		File dexOutputDir = ctx.getDir("newempty", 0);
    		//String dexPath, String optimizedDirectory, String libraryPath, ClassLoader parent
    		/**
    		 * Parameters
				dexFilePath	需要装载的APK或者Jar文件的路径。包含多个路径用File.pathSeparator间隔开,在Android上默认是 ":" 
				optimizedDirectory	优化后的dex文件存放目录，不能为null
				libraryPath	目标类中使用的C/C++库的列表,每个目录用File.pathSeparator间隔开; 可以为 null
				parent	该类装载器的父装载器，一般用当前执行类的装载器
    		 */
    		DexClassLoader dcl = new DexClassLoader(dexFilePath, dexOutputDir.getPath(), null, ctx.getClassLoader());
    		return dcl;
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return null;
    }
    
    public static Class<?> getClass(ClassLoader cl, String clazzStr) {
    	if(cl == null) {
    		return null;
    	}
    	try {
    		Class<?> clazz = cl.loadClass(clazzStr);
    		return clazz;
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
    	return null;
    }
    
    public static Object getClassInstance(Class<?> clazz) {
    	if(clazz == null) {
    		return null;
    	}
    	try {
    		Object o = clazz.newInstance();
    		return o;
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
    	return null;
    }
    
    public static Method getStaticMethod(Class<?> clazz, String methodName, Class<?>[] paramTypes) {
    	if(clazz == null) {
    		return null;
    	}
    	try {
    		return clazz.getMethod(methodName, paramTypes);
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
    	return null;
    }
    
}
